<?php

class HomeController extends Controller {
    protected $allowed = array('administrator' , 'member');

    public function __construct() {
        parent::__construct();
    }

    public function countUnreadNotifications() {
        if ($this->isAllowed()) {
            $notifications = $this->auth->notification;

            $retval = array();
            foreach ($notifications as $notification) {
                if ($notification->status == NotificationModel::$NOTIF_UNREAD)
                    $retval[] = $notification;
            }

            echo count($retval);
        }
    }

    public function getAllWall($pageNo = 1) {
        if ($this->isAllowed()) {
            $walls = Model::loadRowsFromQuery("SELECT * FROM wall WHERE user_id = ".$this->auth->id." ORDER BY id DESC LIMIT ".($pageNo * 1).";" , "WallModel");
            $this->render('home/wallList' , array('walls' => $walls));
        }
    }

    public function index() {
        if ($_POST['searchTerm'] != '') {
            $st = $_POST['searchTerm'];
            $users = Model::loadRowsFromQuery("SELECT * FROM `user` WHERE `username` LIKE '%$st%' OR `fullname` LIKE '%$st%';", 'AuthModel');
            $photos = Model::loadRowsFromQuery("SELECT * FROM `photo` WHERE `access_rule` = 4 AND (`name` LIKE '%$st%' OR `description` LIKE '%$st%' OR `keyword` LIKE '%$st%' OR `category` LIKE '%$st%');", 'PhotoModel');
        }

        if (!$this->isAllowed()) {
            $samplePhotos = Model::loadRowsFromQuery("SELECT * FROM `photo` LIMIT 5;" , 'PhotoModel');
            $this->render("home/guest/index" , array('users' => $users , 'photos' => $photos , 'samplePhotos' => $samplePhotos));
        }
        else {
            $walls = Model::loadRowsFromQuery("SELECT * FROM wall WHERE user_id = ".$this->auth->id." ORDER BY id DESC LIMIT 1" , "WallModel");

			//Get latest, most popular, and highest rating photo
			$top_photos = array();
			$top_photos['latest']	= Model::loadRowsFromQuery("SELECT * FROM `photo` ORDER BY `upload_time` DESC LIMIT 1" , 'PhotoModel');
			$top_photos['rating']	= Model::loadRowsFromQuery("SELECT * FROM `photo` ORDER BY `rating` DESC LIMIT 1" , 'PhotoModel');

			$popular_photo_id		= Model::loadRowsFromQuery("SELECT * FROM `wall` WHERE `photo_id` != 'NULL' GROUP BY `photo_id` ORDER BY COUNT(`photo_id`) DESC" , 'WallModel');
			$top_photos['popular']	= Model::factory('photo', $popular_photo_id[0]->photo_id);

            if (isset($_GET['ajax']))
                $this->render("home/ajax_index" , array('users' => $users , 'photos' => $photos , 'walls' => $walls));
            else
                $this->render("home/index" , array('users' => $users , 'photos' => $photos , 'walls' => $walls, 'top_photos' => $top_photos));
        }
    }

    public function postwall() {
        if ($_POST && $this->isAllowed()) {
            $wall = new WallModel();
            $wall->content = $_POST['wall'];
            $wall->user_id = $this->auth->id;
            $wall->poster_id = $this->auth->id;
            $wall->save();
            $this->redirect($this->createUrl('home/index'));
        }
        else if (!$this->isAllowed()) {
            $this->showError('You are not allowed to open this page');
        }
    }

    public function logout() {
        if ($this->auth != null) {
            $this->auth->logout();
        }
        $this->redirect($this->createUrl("home/index"));
    }

    public function login() {
        if ($_POST) {
            $auth = new AuthModel();
            if($auth->login($_POST['username'] , $_POST['password'])) {
                    $this->redirect($this->createUrl("home/index"));
            }
            else {
                    $this->redirect($this->createUrl("home/index?login_failed=true"));
            }
        }
    }

    public function register() {
        if ($_POST) {
            $user = Model::factory('auth');
            $user->username = $_POST['username'];
            $user->setPassword($_POST['password']);
            $user->fullname = $_POST['name'];
            $user->birthday = $_POST['bday'];
            $user->email = $_POST['email'];
            $user->sex = $_POST['sex'];
            $user->role_id = Model::factoryQuery('auth', "SELECT * FROM role WHERE name= 'member';")->id;
            $user->enabled = true;
            $user->save();
            $user->login($_POST['username'] , $_POST['password']);

            $fb = new FbModel();
            $fb->postToWall($user->fullname . " has just registered to Progin-Foto");
        }
        else if (isset($_GET['code'])) {
            $fb = new FbModel();
            $fb->postToWall("Hi!, I have just registered to Progin-Foto");

            $this->redirect($this->createUrl('home/index'));
        }
    }
	
}

?>
